package doyouknow.group.CnAndZhb.service.impl;

import doyouknow.group.CnAndZhb.entity.TbSceneGwMap;
import doyouknow.group.CnAndZhb.dao.master.TbSceneGwMapDao;
import doyouknow.group.CnAndZhb.error.BusinessException;
import doyouknow.group.CnAndZhb.error.EnumBusinessError;
import doyouknow.group.CnAndZhb.service.TbSceneGwMapService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TbSceneGwMap)表服务实现类
 *
 * @author cn
 * @since 2020-11-03 20:50:41
 */
@Service("tbSceneGwMapService")
public class TbSceneGwMapServiceImpl implements TbSceneGwMapService {
    @Resource
    private TbSceneGwMapDao tbSceneGwMapDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TbSceneGwMap queryById(Integer id) {
        return this.tbSceneGwMapDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<TbSceneGwMap> queryAllByLimit(int offset, int limit) {
        return this.tbSceneGwMapDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<TbSceneGwMap> queryByGM(String mac){
        return this.tbSceneGwMapDao.queryByGM(mac);
    }

    /**
     * 新增数据
     *
     * @param tbSceneGwMap 实例对象
     * @return 实例对象
     */
    @Override
    public TbSceneGwMap insert(TbSceneGwMap tbSceneGwMap) {
        this.tbSceneGwMapDao.insert(tbSceneGwMap);
        return tbSceneGwMap;
    }

    /**
     * 修改数据
     *
     * @param tbSceneGwMap 实例对象
     * @return 实例对象
     */
    @Override
    public TbSceneGwMap update(TbSceneGwMap tbSceneGwMap) {
        this.tbSceneGwMapDao.update(tbSceneGwMap);
        return this.queryById(tbSceneGwMap.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tbSceneGwMapDao.deleteById(id) > 0;
    }

    @Override
    public List<TbSceneGwMap> isSoEasy(String mac) throws BusinessException{
        List<TbSceneGwMap> findObj = tbSceneGwMapDao.queryByMac(mac);
        if(findObj == null){
            throw new BusinessException(EnumBusinessError.GATEWAY_NOT_Exist);
        }


        for (TbSceneGwMap tbSceneGwMap:findObj){
            System.out.println(tbSceneGwMap.getBody());
        }

        return findObj;
    }

}